package cn.xiaopengstack.domain.activity.service.partake;

import cn.xiaopengstack.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import cn.xiaopengstack.domain.activity.model.entity.CreatePartakeEntity;
import cn.xiaopengstack.domain.activity.model.entity.RaffleActivityEntity;
import cn.xiaopengstack.domain.activity.model.entity.UserRaffleOrderEntity;
import cn.xiaopengstack.domain.activity.model.valobj.ActivityStateVO;
import cn.xiaopengstack.domain.activity.repository.IRaffleActivityRepository;
import cn.xiaopengstack.domain.activity.service.IRaffleActivityPartakeService;
import cn.xiaopengstack.domain.strategy.model.entity.UserEntity;
import cn.xiaopengstack.types.enums.ResponseCode;
import cn.xiaopengstack.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @author jiangyangang
 */
@Slf4j
public abstract class AbsRaffleActivityPartakeService implements IRaffleActivityPartakeService {

    protected IRaffleActivityRepository raffleActivityRepository;

    public AbsRaffleActivityPartakeService(IRaffleActivityRepository raffleActivityRepository) {
        this.raffleActivityRepository = raffleActivityRepository;
    }

    @Override
    public UserRaffleOrderEntity createOrder(CreatePartakeEntity createPartakeEntity) {
        RaffleActivityEntity activityEntity = raffleActivityRepository.queryRaffleActivityByActivityId(createPartakeEntity.getActivityId());

        // 校验；活动状态
        if (!ActivityStateVO.open.equals(activityEntity.getState())) {
            throw new AppException(ResponseCode.ACTIVITY_STATUS_ERROR.getCode(), ResponseCode.ACTIVITY_STATUS_ERROR.getInfo());
        }

        // 校验；活动日期「开始时间 <- 当前时间 -> 结束时间」
        if (activityEntity.getBeginDateTime().after(createPartakeEntity.getTime()) || activityEntity.getEndDateTime().before(createPartakeEntity.getTime())) {
            throw new AppException(ResponseCode.ACTIVITY_TIME_ERROR.getCode(), ResponseCode.ACTIVITY_TIME_ERROR.getInfo());
        }

        // 查询用户未使用的订单
        UserRaffleOrderEntity userRaffleOrder = raffleActivityRepository.queryRaffleActivityUnusedPartakeOrderByUid(createPartakeEntity.getActivityId(), createPartakeEntity.getUser().getUserId());
        if (userRaffleOrder != null) {
            userRaffleOrder.setActivityEntity(activityEntity);
            return userRaffleOrder;
        }

        // 构造聚合体
        CreatePartakeOrderAggregate orderAggregate = this.doFilterAccount(activityEntity, createPartakeEntity.getUser(), createPartakeEntity.getTime());

        // 构造订单实体
        UserRaffleOrderEntity userRaffleOrderEntity = this.buildOrder(activityEntity, createPartakeEntity.getUser(), createPartakeEntity.getTime());
        orderAggregate.setUserRaffleOrderEntity(userRaffleOrderEntity);

        // 保存订单
        raffleActivityRepository.saveUserRafflePartakeOrder(orderAggregate);
        return userRaffleOrderEntity;
    }

    /**
     * 创建订单
     * @param activityEntity
     * @param user
     * @param time
     * @return
     */
    protected abstract UserRaffleOrderEntity buildOrder(RaffleActivityEntity activityEntity, UserEntity user, Date time);

    /**
     * 账户过滤返回订单聚合体
     * @param activityEntity
     * @param user
     * @param time
     * @return
     */
    protected abstract CreatePartakeOrderAggregate doFilterAccount(RaffleActivityEntity activityEntity, UserEntity user, Date time);
}
